$(function () {
  var curTable = JSON.parse(localStorage.getItem("curTable"));
  var payCode = localStorage.getItem("payCode");
  //点击返回
  $(".Cashier .title .left").on("click", function () {
    history.go(-1);
  });

  //获取支付方式
  App.ajax("ss_wx/pay_methods", "post", {}, function (res) {
    if (res.flag == true) {
      var payHtml = "";
      $.each(res.info, function (i, v) {
        payHtml += '<li class="' + (payCode ? (v.code == payCode ? "active" : "") : (i == 0 ? "active" : "")) + '" data-paytype=\'' + JSON.stringify(v) + '\'>';
        if (v.code == "ZFBZF") {
          payHtml += '<i class="iconfont icon-swiftpass_alipay_scan"></i>';
        } else if (v.code == "WXZF") {
          payHtml += '<i class="iconfont icon-weixinzhifu-"></i>';
        } else if (v.code == "YHKZF") {
          payHtml += '<i class="iconfont icon-yinhangqiazhifu"></i>';
        } else {
          payHtml += '<i class="iconfont icon-xianjinzhifu"></i>';
        }
        payHtml += v.name + '</li>';
      })
      $(".Cashier .payLists ul").html(payHtml);
      if (payCode == "TSCH") {
        $(".keyBoard").removeClass("hidden");
        $(".saoma").addClass("hidden");
        $("#DiscontMob").addClass("hidden");
        $("#Discount").removeClass("hidden");
      } else {
        $(".keyBoard").addClass("hidden");
        $(".saoma").removeClass("hidden");
        $("#DiscontMob").removeClass("hidden");
        $("#Discount").addClass("hidden");
        if (payCode == "ZFBZF") {
          $(".saoma .ZFFS .iconfont").addClass("icon-swiftpass_alipay_scan").removeClass("icon-weixinzhifu-")
        } else {
          $(".saoma .ZFFS .iconfont").addClass("icon-weixinzhifu-").removeClass("icon-swiftpass_alipay_scan")
        }
      }
    } else {
      layer.open({
        content: res.msg,
        skin: 'msg',
        time: 2 //2秒后自动关闭
      })
    }
  })

  //获取订单金额 桌台号
  $(".bill .tNum").html("桌台：" + curTable.name);
  $("#billMon,#receivMon").html((curTable.totalInfo.totalMon * 1).toFixed(1));
  $("#inputMon").find(".lastGB").before("<span class='num'>" + curTable.totalInfo.totalMon * 1 + "</span>")
  $(".saoma .FKM_je").val("￥" + curTable.totalInfo.totalMon * 1);
  //点击查看订单
  $(".Cashier .bill .checkMenu").on("click", function () {
    var menuHtml = "";
    $.each(curTable.cart, function (i, v) {
      menuHtml += "<li><span>" + v.name + "</span><span>" + (v.price * 1).toFixed(1) + "</span><span>" + (v.num * 1).toFixed(1) + "</span><span>" + (v.price * v.num).toFixed(1) + "</span></li>"
    });
    $("#MenuModal .menuLists").html(menuHtml);
  })
  //输入折扣，改变应收金额
  $("#Discount").on("DOMNodeInserted", function () {
    var billMon = $("#billMon").html();
    var inps = $("#Discount").find('.num');
    var nums = "";
    inps.each(function (j, k) {
      nums += $(this).html();
    })
    // console.log(nums);
    //改变应收金额
    $("#receivMon").html((billMon * (nums / 100)).toFixed(1));
    $(".saoma .FKM_je").val("￥" + (billMon * (nums / 100)).toFixed(1));
    $("#inputMon .num").html((billMon * (nums / 100)).toFixed(1));
  })
  //移动支付时输入折扣，改变应收金额
  $("#DiscontMob input").on("input propertychange", function () {
    var billMon = $("#billMon").html();
    var nums = $("#DiscontMob input").val();
    console.log(nums);

    //改变应收金额
    if (nums) {
      $("#receivMon").html((billMon * (nums / 100)).toFixed(1));
      $(".saoma .FKM_je").val("￥" + (billMon * (nums / 100)).toFixed(1));
    } else {
      $("#receivMon").html((billMon * 1).toFixed(1));
      $(".saoma .FKM_je").val("￥" + (billMon * 1).toFixed(1));
    }
  })
  //获取折扣输入框的值
  function getDiscount(id) {
    var inps = $(id).find('.num');
    var nums = "";
    inps.each(function (j, k) {
      nums += $(this).html();
    })
    return nums;
  }
  //输入抹零金额，改变应收金额
  $("#MLJE").on("DOMNodeInserted", function () {
    var billMon = $("#billMon").html();
    var inps = $("#MLJE").find('.num');
    var nums = "";
    inps.each(function (j, k) {
      nums += $(this).html();
    })
    // console.log(nums);
    //改变应收金额
    $("#receivMon").html();
  })
  //抹零选项改变
  $("#MLRadio input").on("change", function () {
    if ($(this).prop("checked")) {
      // console.log($(this).val());
    };
  })

  //输入实收金额，改变找零金额
  $("#inputMon").on("DOMNodeInserted", function () {
    var inps = $("#inputMon").find('.num');
    var nums = "";
    inps.each(function (j, k) {
      nums += $(this).html();
    })
    // console.log(nums);
    //获取应收金额
    var SSMon = $("#receivMon").html();
    var ZLMon = nums - SSMon;
    if (ZLMon < 0) {
      return;
    }
    //改变找零金额
    $("#ZLMon").html(ZLMon.toFixed(1))
  })


  //支付方式切换
  $(".payLists ul").on("click", "li", function () {
    var payType = $(this).data("paytype");
    $(this).addClass("active").siblings().removeClass("active");
    if (payType.code == "TSCH") {
      $(".keyBoard").removeClass("hidden");
      $(".saoma").addClass("hidden");
      $("#DiscontMob").addClass("hidden")
      $("#Discount").removeClass("hidden")
    } else {
      $(".keyBoard").addClass("hidden");
      $(".saoma").removeClass("hidden");
      $("#DiscontMob").removeClass("hidden")
      $("#Discount").addClass("hidden")
      if (payType.code == "ZFBZF") {
        $(".saoma .ZFFS .iconfont").addClass("icon-swiftpass_alipay_scan").removeClass("icon-weixinzhifu-")
      } else {
        $(".saoma .ZFFS .iconfont").addClass("icon-weixinzhifu-").removeClass("icon-swiftpass_alipay_scan")
      }
    }
  });
  //移动支付
  $(".saoma .QDSY_btn").off("click").on("click", function () {
    var paytype = $(".Cashier .payLists ul li.active").data("paytype");
    var pay_info = [];
    curTable.cart.forEach(function (v, i) {
      var obj = {
        p_id: v.p_id,
        p_num: v.num,
        // c_id:v.c_id 
      };
      pay_info.unshift(obj);
    });
    var order_id = "";
    var online_pay_flag = "";
    if (payCode) {
      order_id = App.guid();
      online_pay_flag = 0;
    } else {
      order_id = curTable.order_id;
      online_pay_flag = 1;
    }
    var money = $("#receivMon").html() * 1;
    if (paytype.id == 11) {
      App.ajax("shop_wechat/bilalipay/scan/order", "post", {
        'money': money,
        'auth_code': $(".FKM_input").val(),
        'shop_id': 1,
        'out_trade_no': order_id,
        "product_name": "点餐菜品",
        "online_pay_flag": online_pay_flag,
        "pos_order": JSON.stringify({
          "order_id": order_id,
          "pay_method": [{
            "pay_id": paytype.id,
            "money": $("#billMon").html(),
          }],
          "discount": $("#DiscontMob input").val(),
          "need_to_pay": $("#receivMon").html(),
          "customer_pay": $("#receivMon").html(),
          "zhaoLing": "",
          "moling": "",
          "pay_info": pay_info,
          "p_total": curTable.totalInfo.totalMon,
          "floor_id": curTable.floor_id,
          "table_id": curTable.table_id
        })
      }, function (res) {
        if (res.return_code == "SUCCESS") {
          var tableInfo = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          curTable.state = 4;
          $.each(tableInfo, function (j, k) {
            if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
              k.state = 4;
              k.cart = curTable.cart;
              k.totalInfo = curTable.totalInfo;
            }
          });
          localStorage.setItem("tableInfos", JSON.stringify(tableInfo));
          localStorage.setItem('curTable', JSON.stringify(curTable));
          localStorage.removeItem("payCode");
          layer.open({
            content: "支付成功",
            skim: 'msg',
            time: 1,
            success: function () {
              setTimeout(function () {
                location.href = "../pages/Table.html";
              }, 1000);
            }
          })
        } else {
          layer.open({
            content: res.return_msg,
            skin: 'msg',
            time: 2 //2秒后自动关闭
          })
        }
      })
    }
    if (paytype.id == 12) {
      App.ajax("payment/bilalipay/scancode", "post", {
        'total': 0.01,
        'auth_code': $(".FKM_input").val(),
        'shopid': 1,
        'bill': order_id,
        'subject': "order",
        "online_pay_flag": online_pay_flag,
        "pos_order": JSON.stringify({
          "order_id": order_id,
          "pay_method": [{
            "pay_id": paytype.id,
            "money": $("#billMon").html(),
          }],
          "discount": $("#DiscontMob input").val(),
          "need_to_pay": $("#receivMon").html(),
          "customer_pay": $("#receivMon").html(),
          "zhaoLing": "",
          "moling": "",
          "pay_info": pay_info,
          "p_total": curTable.totalInfo.totalMon,
          "floor_id": curTable.floor_id,
          "table_id": curTable.table_id
        })
      }, function (res) {
        if (res.return_code == "SUCCESS") {
          var tableInfo = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          curTable.state = 4;
          $.each(tableInfo, function (j, k) {
            if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
              k.state = 4;
              k.cart = curTable.cart;
              k.totalInfo = curTable.totalInfo;
            }
          });
          localStorage.setItem("tableInfos", JSON.stringify(tableInfo));
          localStorage.setItem("curTable", JSON.stringify(curTable));
          localStorage.removeItem("payCode");
          layer.open({
            content: "支付成功",
            skim: 'msg',
            time: 1,
            success: function () {
              setTimeout(function () {
                location.href = "../pages/Table.html";
              }, 1000);
            }
          })
        } else {
          layer.open({
            content: res.return_msg,
            skin: 'msg',
            time: 2 //2秒后自动关闭
          })
        }
      })
    }
  })

  //添加光标
  clickInput("Discount", "请输入折扣");
  clickInput("MLJE", "0.0");
  clickInput("inputMon", "0.0");
  // 点击输入数字
  function clickInput(input, text) {
    $("#" + input).off("mouseenter").on("mouseenter", function () {
      $(this).find(".virtualPlac").addClass("hidden");
      $(".virtualInput").each(function (i, v) {
        if ($(this).attr("id") == input) {
          $(this).find(".lastGB").removeClass("hidden");
        } else {
          $(this).find(".lastGB").addClass("hidden");
        }
      })
      clickBoard(input, text);

    })
    clickBoard(input, text);
  }

  //点击数据键盘
  function clickBoard(input, text) {
    $(".Cashier .keyBoard .board").off("click").on("click", ".item", function () {
      var num = $(this).data("num") + "";
      if (num == "empty") {
        $("#" + input).html("<span class='virtualPlac'>" + text + "</span><span class='lastGB hidden'></span>");
      } else if (num == "del") {
        $("#" + input).find(".lastGB").prev().remove();
        var inputNum = $("#" + input).find('.num').html();
        if (!inputNum) {
          $("#" + input).html("<span class='virtualPlac'>" + text + "</span><span class='lastGB hidden'></span>");
        }
      } else if (num == "checkOut") {
        var pay_info = [];
        curTable.cart.forEach(function (v, i) {
          var obj = {
            p_id: v.p_id,
            p_num: v.num,
            // c_id:v.c_id 
          };
          pay_info.unshift(obj);
        })
        // console.log(pay_info);
        var pay_id = $(".Cashier .payLists ul li.active").data("paytype").id;
        //点击确定收银
        // if (payCode&&!curTable.order_id) {
        if (!curTable.order_id) {
          App.ajax("ss_wx/hzg_orders_and_pay", "post", {
            "pay_flag": 0,
            "pos_order": JSON.stringify({
              "order_id": App.guid(),
              "pay_method": [{
                "pay_id": pay_id,
                "money": getDiscount("#inputMon"),
              }],
              "discount": getDiscount("#Discount"),
              "need_to_pay": $("#receivMon").html(),
              "customer_pay": getDiscount("#inputMon"),
              "moling": "",
              "zhaoLing": $("#ZLMon").html(),
              "pay_info": pay_info,
              "p_total": curTable.totalInfo.totalMon,
              "floor_id": curTable.floor_id,
              "table_id": curTable.table_id
            })
          }, function (res) {
            if (res.flag == true) {
              var tableInfo = JSON.parse(localStorage.getItem("tableInfos") || "[]");
              curTable.state = 4;
              $.each(tableInfo, function (j, k) {
                if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                  k.state = 4;
                  k.cart = curTable.cart;
                  k.totalInfo = curTable.totalInfo;
                }
              });
              localStorage.setItem("tableInfos", JSON.stringify(tableInfo));
              localStorage.setItem("curTable", JSON.stringify(curTable));
              localStorage.removeItem("payCode");
              layer.open({
                content: "支付成功",
                skin: 'msg',
                time: 2, //2秒后自动关闭
                success: function () {
                  setTimeout(function () {
                    location.href = "../pages/Table.html";
                  }, 1000);
                }
              })
            } else {
              layer.open({
                content: res.msg,
                skin: 'msg',
                time: 2 //2秒后自动关闭
              })
            }
          })
        } else {
          // {
          //   // "pay_flag": 0,
          //   "pos_order": JSON.stringify({
          //     "order_id": curTable.order_id ? curTable.order_id : App.guid(),
          //     "pay_method": [{
          //       "pay_id": pay_id,
          //       "money": curTable.totalInfo.totalMon,
          //     }],
          //     // "pay_info": pay_info,
          //     "p_total": curTable.totalInfo.totalMon,
          //     "floor_id": curTable.floor_id,
          //     "table_id": curTable.table_id
          //   })
          // }
          App.ajax("ss_wx/hzg_orders_pay", "post", {
            "pay_flag": 0,
            "pos_order": JSON.stringify({
              "order_id": curTable.order_id ? curTable.order_id : App.guid(),
              "pay_method": [{
                "pay_id": pay_id,
                "money": getDiscount("#inputMon"),
              }],
              "discount": getDiscount("#Discount"),
              "need_to_pay": $("#receivMon").html(),
              "customer_pay": getDiscount("#inputMon"),
              "moling": "",
              "zhaoLing": $("#ZLMon").html(),
              "pay_info": pay_info,
              "p_total": curTable.totalInfo.totalMon,
              "floor_id": curTable.floor_id,
              "table_id": curTable.table_id
            })
          }, function (res) {
            // console.log(res)
            if (res.flag == true) {
              var tableInfo = JSON.parse(localStorage.getItem("tableInfos") || "[]");
              curTable.state = 4;
              $.each(tableInfo, function (j, k) {
                if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                  k.state = 4;
                  k.cart = curTable.cart;
                  k.totalInfo = curTable.totalInfo;
                }
              });
              localStorage.setItem("tableInfos", JSON.stringify(tableInfo));
              localStorage.setItem("curTable", JSON.stringify(curTable));
              localStorage.removeItem("payCode");
              layer.open({
                content: "支付成功",
                skin: 'msg',
                time: 2, //2秒后自动关闭
                success: function () {
                  setTimeout(function () {
                    location.href = "../pages/Table.html";
                  }, 1000);
                }
              })
            } else {
              layer.open({
                content: res.msg,
                skin: 'msg',
                time: 2 //2秒后自动关闭
              })
            }
          })
        }

      } else if (num.indexOf("元") > -1) {
        var mon = parseFloat(num);
        var inps = $("#" + input).find('.num');
        var nums = "";
        inps.each(function (j, k) {
          nums += $(this).html();
        })
        if (!$("#" + input).find('.num').html()) {
          $("#" + input).find(".virtualPlac").addClass("hidden");
        }
        $("#" + input).find(".num").remove();
        $("#" + input).find(".lastGB").before("<span class='num'>" + (mon + nums * 1) + "</span>");
      } else {
        if (!$("#" + input).find('.num').html()) {
          $("#" + input).find(".virtualPlac").addClass("hidden");
        }
        $("#" + input).find(".lastGB").before("<span class='num'>" + num + "</span>");
        // <span class='GB'></span>
      }
    })
  }
})
//光标闪烁
GBstar();

function GBstar() {
  var flag = true;

  function star(Name) {
    flag = !flag;
    if (flag) {
      $(".lastGB").css("background", "none");
    } else {
      $(".lastGB").css("background", "#EC4321");
    }
  }
  setInterval(function () {
    star()
  }, 500)
}